package com.gqr.threadcoreknowledge.stopthreads;


/*
* run方法内没有sleep或者wait方法时，停止线程
* */
public class RightWayStopThreadWithoutSleep  implements Runnable{
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
        thread.start();

        Thread.sleep(1000);
        /*
        * 只是用interrupt  是没有响应的
        * 需要在run方法内 增加判断  Thread.currentThread().isInterrupted()
        * */
        thread.interrupt();
    }

    @Override
    public void run() {
        int num = 0;
        /*
        *
        * */
        while (!Thread.currentThread().isInterrupted() &&num<= Integer.MAX_VALUE / 2){
            if(num %100==0){
                System.out.println(num+"是100的倍数");
            }
            num++;
        }
        System.out.println("状态"+Thread.currentThread().isInterrupted());
        System.out.println("任务运行结束了");
    }
}
